老王是王牌麵館的老主顧,每次去都只會點滷肉飯、燙青菜跟豆乾海帶一盤,當他去店裡跟老闆說點跟上次一樣的餐點,但老闆始終不記得。於是聰明的老闆想到給老王一張貴賓卡上面記載老王的會員編號,老王的用餐習慣就記載他的電腦裡,只要老王下次來刷一下貴賓卡就可以直接點餐,是不是很方便呢
因為Http是無狀態的記不住客戶喜好,只好透過把貴賓卡(Cookie)放在老王的身上,把客戶喜好存在電腦(Sesson)裡。
Cookie是由Web Server產生放在瀏覽器的一小份資訊,第一次創建後的每次訪問該Web Server都會被攜帶過去。
放置cookie
@WebServlet("/CookieServlet")
public class CookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//創建Cookie
Cookie cookie1 = new Cookie("k1", "value1");
Cookie cookie2 = new Cookie("k2", "value2");
resp.addCookie(cookie1);
resp.addCookie(cookie2);
}
}
取得cookie
@WebServlet("/GetCookieServlet")
public class GetCookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
System.out.println(cookie.getName() + " : " + cookie.getValue());
}
}
}
}
先訪問
再訪問
cookie默認是一次會話範圍內,可以透過Cookie的SetMaxAge()方法讓Cookie保留在瀏覽器中。
設置維持久化cookie
@WebServlet("/CookieSetMaxServlet")
public class CookieSetMaxServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res){
Cookie cookie = new Cookie("CookieSetMax", "CookieSetMaxValue");
//api second
cookie.setMaxAge(60*5);
res.addCookie(cookie);
}
}
重啟tomcat,先訪問/CookieSetMaxServlet
再訪問/CookieServlet
再訪問/GetCookieServlet
關掉瀏覽器再重開瀏覽器訪問/GetCookieServlet,只剩CookieSetMax
也可以針對訪問路徑才攜帶Cookie
@WebServlet("/SetPathCookieServlet")
public class SetPathCookieServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res){
Cookie cookie = new Cookie("SetPathCookie", "SetPathCookieValue");
cookie.setPath("/CookieServlet");
res.addCookie(cookie);
}
}
先訪問/SetPathCookieServlet
再訪問/CookieSetMaxServlet,未帶SetPathCookie
最後訪問/CookieServlet
Web Server會為每個客戶開一塊空間即Session Object透過JESSIONID,Server就可以紀錄與查找客戶的狀態了。
創建SessionServlet
@WebServlet("/SessionServlet")
public class SessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("=====GetSessopnDataServlet=====");
String username = req.getParameter("username");
HttpSession session = req.getSession();
System.out.println(session.isNew());
System.out.println(session.getId());
session.setAttribute("username", username);
}
}
創建GetSessopnDataServlet
@WebServlet("/GetSessopnDataServlet")
public class GetSessopnDataServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("=====GetSessopnDataServlet=====");
HttpSession session = req.getSession();
System.out.println(session.isNew());
System.out.println(session.getId());
System.out.println("data:"+session.getAttribute("username"));
}
}
先訪問SessionServlet
再訪問http://localhost:8080/GetSessopnDataServlet?username=james
<web-app>
<display-name>Archetype Created Web Application</display-name>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
</web-app>
@WebServlet("/SessionSetMaxServlet")
public class SessionSetMaxServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res){
System.out.println("=====SessionSetMaxServlet=====");
HttpSession session = req.getSession();
session.setMaxInactiveInterval(300);
//let session fail
//session.invalidate();
}
}
在tomcat資料夾下conf/web.xml中可以查到預設時間,當你設置在專案內WEB-INFO下會以此優先